﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Diagnostics;

namespace TestObjectsHandler
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            OpenFileDialog openfiledialog = new OpenFileDialog();
            openfiledialog.Filter = "ALG EXE File (*.exe) | *.exe";
            openfiledialog.FilterIndex = 0;
            openfiledialog.RestoreDirectory = true;
            // openfiledialog.CreatePrompt = true;
            openfiledialog.Title = "Select ALG EXE File";
            openfiledialog.ShowDialog();

            if (openfiledialog.FileName.IndexOf(":") < 0)
            {   // if click cancel
                return;
            }

            // get the file full directory
            string str;
            str = openfiledialog.FileName.ToString();
            textBox2.Text = str;
        }

        private void button2_Click(object sender, EventArgs e)
        {
            Process cmd = new Process();
            cmd.StartInfo.FileName = "cmd";
            cmd.StartInfo.RedirectStandardOutput = true;
            cmd.StartInfo.RedirectStandardInput = true;
            cmd.StartInfo.UseShellExecute = false;
            cmd.StartInfo.CreateNoWindow = false;

            cmd.Start();
            
            // remove some files
            cmd.StandardInput.WriteLine("C:");
            cmd.StandardInput.WriteLine("cd \\");
            // cmd.StandardInput.WriteLine("echo we are going to remove some files.");

            if (checkBox1.Checked == true)
            { 
                // remove mdb file
                cmd.StandardInput.WriteLine("del /q " + textBox1.Text + "\\Dataio\\Tlwin\\System\\RoadRunner.mdb");
            }
            if (checkBox2.Checked == true)
            {
                // remove dll file
                //cmd.StandardInput.WriteLine("del " + textBox1.Text + "\\Dataio\\Tlwin\\AlgDlls\\StdNand.dll");
                //cmd.StandardInput.WriteLine("del " + textBox1.Text + "\\Dataio\\Tlwin\\AlgDlls\\LargeFormatNand.dll");
                //cmd.StandardInput.WriteLine("del " + textBox1.Text + "\\Dataio\\Tlwin\\AlgDlls\\PIC*.dll");
                cmd.StandardInput.WriteLine("del /q " + textBox1.Text + "\\Dataio\\Tlwin\\AlgDlls\\*.dll");
            }
            if (checkBox3.Checked == true)
            {
                // remove fpga file
                // cmd.StandardInput.WriteLine("del " + textBox1.Text + "\\Dataio\\Tlwin\\System\\f110_fc2.bit");
                cmd.StandardInput.WriteLine("del /q " + textBox1.Text + "\\Dataio\\Tlwin\\System\\*fc2.bit");
                cmd.StandardInput.WriteLine("del /q " + textBox1.Text + "\\Dataio\\Tlwin\\System\\*fc3.bit");
                cmd.StandardInput.WriteLine("del /q " + textBox1.Text + "\\Dataio\\Tlwin\\System\\*LCA.bit");
            }
            if (checkBox4.Checked == true)
            {
                // remove H/W elf file
                //cmd.StandardInput.WriteLine("cd \\Dataio\\Tlwin\\FlashPAK\\fp_sys\\");
                //cmd.StandardInput.WriteLine("echo remove HWData_C elf > HWData.txt");
                cmd.StandardInput.WriteLine("del /q " + textBox1.Text + "\\Dataio\\Tlwin\\FlashPAK\\fp_sys\\HWData_C.elf");
                //cmd.StandardInput.WriteLine("del " + textBox1.Text + "\\Dataio\\Tlwin\\FlashPAK\\fp_sys\\HWData*.*");
            }

            if (textBox2.Text == "")
            {
                cmd.StandardInput.WriteLine("exit");
                cmd.Close();
                MessageBox.Show("please select new ALG EXE to install");
            }
            else
            {
                // install new algorithm packages
                cmd.StandardInput.WriteLine("exit");
                cmd.Close();
                
                cmd.Start();
                cmd.StandardInput.WriteLine("\"" + textBox2.Text + "\"");
                // MessageBox.Show("chris.");
                cmd.StandardInput.WriteLine("exit");
                // cmd.WaitForExit();
                // info = cmd.StandardOutput.ReadToEnd();
                // richTextBox1.AppendText(info);
                cmd.Close();
            }
            
        }        
    }
}
